package com.geilizu.dom.service.impl;

import com.geilizu.dom.dto.*;
import com.geilizu.dom.entity.Dormitory;
import com.geilizu.dom.dao.DormitoryDao;
import com.geilizu.dom.service.DormitoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Dormitory)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 17:27:05
 */
@Service("dormitoryService")
public class DormitoryServiceImpl implements DormitoryService {
    @Resource
    private DormitoryDao dormitoryDao;


    @Override
    public List<Dormitory> list(Integer id) {

        return dormitoryDao.list(id);
    }


    @Override
    public List<Dormitory> queryAll() {
        return dormitoryDao.queryAll();
    }

    @Override
    public Integer save(Dormitory dormitory) {
        return dormitoryDao.save(dormitory);
    }

    @Override
    public Map<String, Object> listDo(Integer page, Integer size) {
        page=(page-1)*size;
        List<DormitoryDto> dtoList=dormitoryDao.listDo(page,size);
        Long count=dormitoryDao.listDoCount();
        Map<String,Object> map=new HashMap<>();
        map.put("dormitoryList",dtoList);
        map.put("count",count);
        return map;
    }

    @Override
    public Map<String, Object> search(SearchDto searchDto) {
        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
        List<DormitoryDto> absentList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getValue().equals("")) {
            absentList = dormitoryDao.listDo(page, searchDto.getSize());
            count = dormitoryDao.listDoCount();
        } else if (searchDto.getKey().equals("name")) {
            DoSearchDto search=new DoSearchDto(page,searchDto.getSize(),searchDto.getValue(),"");
            absentList = dormitoryDao.queryByValue(search);
            count = dormitoryDao.queryByValueCount(search);
        } else if (searchDto.getKey().equals("telephone")) {
            DoSearchDto search=new DoSearchDto(page,searchDto.getSize(),"",searchDto.getValue());
            absentList = dormitoryDao.queryByValue(search);
            count = dormitoryDao.queryByValueCount(search);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dormitoryList", absentList);
        map.put("count", count);
        return map;
    }

    @Override
    public Dormitory findById(Integer id) {
        return dormitoryDao.findById(id);
    }

    @Override
    public Integer updateById(Dormitory dormitory) {
        return dormitoryDao.updateById(dormitory);
    }
}
